<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\CourseSourceTable;
use App\Admin\Repositories\Course;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CourseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Course(['class2']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id');
            $grid->column('name');
//            $grid->column('class_id_1');
            $grid->column('class2.name', '分类');
            $grid->column('cover')->image(' ', 50);
            $grid->column('watch_num');
            $grid->column('pay_amount', '价格')->display(function ($v) {
                return fen_to_yuan($v);
            });
            $grid->column('vip_pay_amount', '会员价')->display(function ($v) {
                return fen_to_yuan($v);
            });
            $grid->column('status', '状态')->switch();
            $grid->column('is_recommend', '是否推荐')->switch();
            $grid->column('sort');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(3);
                $filter->like('name')->width(3);
                $filter->equal('class_id_2', '分类')->select(\App\Models\CourseClass::where('pid', '>', 0)->where('status', 1)->pluck('name', 'id')->all())->width(3);
                $filter->between('created_at')->datetime()->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Course(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('class_id_1');
            $show->field('class_id_2');
            $show->field('cover');
            $show->field('banners');
            $show->field('details');
            $show->field('watch_num');
            $show->field('is_pay');
            $show->field('pay_amount');
            $show->field('vip_is_pay');
            $show->field('vip_pay_amount');
            $show->field('status');
            $show->field('is_recommend');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Course(['video']), function (Form $form) {
            $form->tab('基本设置', function (Form $form) {
                $form->display('id');
                $form->text('name')->required();
                $form->hidden('class_id_1');
                $form->select('class_id_2', '课程分类')->options(\App\Models\CourseClass::where('status', 1)->where('pid', '>', 0)->pluck('name', 'id')->all())->required();
                $form->number('watch_num')->default(0);
                $form->switch('status')->default(1);
                $form->switch('is_recommend')->default(0);
                $form->image('cover')->uniqueName()->autoUpload()->saveFullUrl()->required();
                $form->multipleImage('banners')->sortable()->uniqueName()->autoUpload()->saveFullUrl()->saving(function ($v) {
                    return implode(',', $v);
                })->customFormat(function ($v) {
                    return $v;
                })->required();
                $form->ueditor('details')->required();
            })->tab('素材选择', function (Form $form) {
                $form->hasMany('video', '素材', function ($from) {
                    $from->selectTable('course_source_id', '素材')
                        ->title('选择素材')
                        ->from(CourseSourceTable::make())
                        ->model(\App\Models\CourseSource::class, 'id', 'name')
                        ->required();
                    $from->number('sort')->default(0);
                });

            })->tab('价格设置', function (Form $form) {
                $form->radio('is_pay', '付费方式')->options(['免费', '付费'])->default(0)
                    ->when(1, function (Form $form) {
                        $form->text('pay_amount', '购买金额')->customFormat(function ($v) {
                            return fen_to_yuan($v);
                        });
                        $form->radio('vip_is_pay', '会员付费方式')->options(['免费', '付费'])->default(0)
                            ->when(1, function (Form $form) {
                                $form->text('vip_pay_amount', '会员购买金额')->customFormat(function ($v) {
                                    return fen_to_yuan($v);
                                });
                            });
                    });
            })->saving(function ($form) {

                if ($form->class_id_2) {
                    $form->class_id_1 = \App\Models\CourseClass::where('id', $form->class_id_2)->value('pid') ?: 0;

                    if ($form->is_pay) {
                        if (!$form->pay_amount || $form->pay_amount <= 0) return $form->response()->error('购买金额需大于0');
                        if (1 != preg_match('/^\d+(\.\d{1,2})?$/', $form->pay_amount)) return $form->response()->error('购买金额格式错误');
                        $form->pay_amount = yuan_to_fen($form->pay_amount);

                        if ($form->vip_is_pay) {
                            if (!$form->vip_pay_amount || $form->vip_pay_amount <= 0) return $form->response()->error('会员购买金额需大于0');
                            if (1 != preg_match('/^\d+(\.\d{1,2})?$/', $form->vip_pay_amount)) return $form->response()->error('会员购买金额格式错误');
                            $form->vip_pay_amount = yuan_to_fen($form->vip_pay_amount);
                        } else {
                            $form->vip_pay_amount = 0;
                        }
                    } else {
                        $form->pay_amount = 0;
                        $form->vip_is_pay = 0;
                        $form->vip_pay_amount = 0;
                    }
                }

            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
